<?php

namespace common\models\c2\entity;

use common\helpers\CodeGenerator;
use Yii;

/**
 * This is the model class for table "{{%amoeba_form_item}}".
 *
 * @property string $id
 * @property string $amoeba_id
 * @property string $amoeba_form_id
 * @property string $seo_code
 * @property string $user_id
 * @property string $label
 * @property integer $state
 * @property integer $status
 * @property integer $positionF
 * @property string $created_at
 * @property string $updated_at
 */
class AmoebaFormItem extends \cza\base\models\ActiveRecord
{
    /**
     * 死逼玩意无端端提示name属性不存在，被逼加上去才不报错
     * @var
     */
    public $name;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%amoeba_form_item}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['amoeba_id', 'amoeba_form_id', 'user_id', 'position'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['amoeba_id', 'amoeba_form_id', 'user_id'], 'required'],
            [['seo_code', 'label'], 'string', 'max' => 255],
            [['state', 'status'], 'integer', 'max' => 4],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'amoeba_id' => Yii::t('app.c2', 'Amoeba ID'),
            'amoeba_form_id' => Yii::t('app.c2', 'Amoeba Form ID'),
            'seo_code' => Yii::t('app.c2', 'Seo Code'),
            'user_id' => Yii::t('app.c2', 'User ID'),
            'label' => Yii::t('app.c2', 'Label'),
            'state' => Yii::t('app.c2', 'State'),
            'status' => Yii::t('app.c2', 'Status'),
            'position' => Yii::t('app.c2', 'Position'),
            'created_at' => Yii::t('app.c2', 'Created At'),
            'updated_at' => Yii::t('app.c2', 'Updated At'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\AmoebaFormItemQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\AmoebaFormItemQuery(get_called_class());
    }

    /**
     * setup default values
     **/
    public function loadDefaultValues($skipIfSet = true)
    {
        parent::loadDefaultValues($skipIfSet);
        if ($this->isNewRecord) {
            $this->seo_code = CodeGenerator::getAmoebaFormItemCode($this, 'ZF');
        }
    }

    public function getUser()
    {
        return $this->hasOne(FeUser::className(), ['id' => 'user_id']);
    }

    public function getAmoebaForm()
    {
        return $this->hasOne(AmoebaForm::className(), ['id' => 'amoeba_form_id']);
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        // if ($insert) {
        //     $userAmoebaRs = UserAmoebaRs::findOne(['user_id' => $this->user_id]);
        //     if (is_null($userAmoebaRs)) {
        //         $userAmoebaRs = new UserAmoebaRs();
        //         $userAmoebaRs->loadDefaultValues();
        //         $userAmoebaRs->user_id = $this->user_id;
        //         $userAmoebaRs->amoeba_id = $this->amoeba_id;
        //         $userAmoebaRs->save();
        //     }
        // }
    }

    public function getAmoeba()
    {
        return $this->hasOne(Amoeba::className(), ['id' => 'amoeba_id']);
    }

}
